﻿using ComponentTracer;
using log4net;
using StructureMap;

namespace ComponentLoader
{
    public class StructureMapComponentLoader<TEntryPoint>
    {
        private Container container;

        public StructureMapComponentLoader() {
            Log4Net.Setup();
        }

        public void Load(bool withTracing) {
            var logger = LogManager.GetLogger(GetType().Name);
            container = new Container(
                c => {
                    c.Scan(scanner => {
                        scanner.AssembliesFromApplicationBaseDirectory();
                        scanner.WithDefaultConventions();
                        scanner.AddAllTypesOf<TEntryPoint>();
                    });
                    if (withTracing) {
                        c.RegisterInterceptor(new TracingTypeInterceptor(logger.Debug));
                    }
                });
        }

        public TEntryPoint InstanciateEntryPoint() {
            return container.GetInstance<TEntryPoint>();
        }
    }
}